package com.uni.crawl.controller.crawl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uni.crawl.common.annotation.PreventDuplicateSubmit;
import com.uni.crawl.common.result.PageResult;
import com.uni.crawl.common.result.Result;
import com.uni.crawl.model.form.crawl.CrawlNamespaceForm;
import com.uni.crawl.model.query.crawl.CrawlNamespacePageQuery;
import com.uni.crawl.model.vo.crawl.CrawlNamespaceNameVO;
import com.uni.crawl.model.vo.crawl.CrawlNamespaceVO;
import com.uni.crawl.service.crawl.CrawlNamespaceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "爬虫空间管理接口")
@RestController
@RequestMapping("/api/v1/crawl/namespace")
@RequiredArgsConstructor
public class CrawlNamespaceController {

    private final CrawlNamespaceService crawlNamespaceService;

    @Operation(summary = "爬虫空间分页列表")
    @PostMapping("/page")
    public PageResult<CrawlNamespaceVO> getDictPage(@RequestBody CrawlNamespacePageQuery pageQuery) {
        Page<CrawlNamespaceVO> result = crawlNamespaceService.getPage(pageQuery);
        return PageResult.success(result);
    }

    @Operation(summary = "爬虫空间下拉列表")
    @GetMapping("/nameList")
    public Result<List<CrawlNamespaceNameVO>> nameList() {
        List<CrawlNamespaceNameVO> result = crawlNamespaceService.nameList();
        return Result.success(result);
    }

    @Operation(summary = "爬虫空间表单数据")
    @GetMapping("/{id}/form")
    public Result<CrawlNamespaceForm> getDictForm(
            @Parameter(description = "ID") @PathVariable Long id) {
        CrawlNamespaceForm formData = crawlNamespaceService.getForm(id);
        return Result.success(formData);
    }

    @Operation(summary = "新增爬虫空间", security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping("add")
    @PreAuthorize("@ss.hasPerm('crawl:namespace:add')")
    @PreventDuplicateSubmit
    public Result<Boolean> add(@RequestBody CrawlNamespaceForm form) {
        return Result.judge(crawlNamespaceService.add(form));
    }

    @Operation(summary = "修改爬虫空间")
    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPerm('crawl:namespace:edit')")
    public Result<Boolean> update(@PathVariable Long id, @RequestBody CrawlNamespaceForm form) {
        return Result.judge(crawlNamespaceService.update(id, form));
    }

    @Operation(summary = "删除爬虫空间")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPerm('crawl:namespace:delete')")
    public Result<Boolean> delete(@PathVariable Long id) {
        return Result.judge(crawlNamespaceService.removeById(id));
    }

}
